Timer = class()

Timer.NS_PER_SECOND = 10000000
Timer.MAX_NS_PER_UPDATE = 10000000
Timer.MAX_TICKS_PER_UPDATE = 100

function Timer:init(ticksPerSecond)
    self.ticksPerSecond = ticksPerSecond
    self.lastTime = Timer.getNanoTime()
    self.timeScale = 1.0
    self.passedTime = 0
    self.elapsedTime = 0
end

function Timer:advanceTime()
    local now = ElapsedTime * Timer.NS_PER_SECOND
    local passedNs = now - self.lastTime
    self.lastTime = now
    
    if passedNs < 0 then passedNs = 0 end
    if passedNs > Timer.MAX_NS_PER_UPDATE then passedNs = Timer.MAX_NS_PER_UPDATE end
    
    self.fps   = passedNs / Timer.NS_PER_SECOND
    self.passedTime = self.passedTime + passedNs * self.timeScale * self.ticksPerSecond / Timer.NS_PER_SECOND;
    self.ticks = math.floor(self.passedTime)
    if self.ticks > Timer.MAX_TICKS_PER_UPDATE then self.ticks = Timer.MAX_TICKS_PER_UPDATE end
    self.passedTime = self.passedTime - self.ticks
    self.elapsedTime = self.elapsedTime + DeltaTime * self.timeScale
    self.a = self.passedTime
end

function Timer.getNanoTime()
    return ElapsedTime * Timer.NS_PER_SECOND
end

function Timer:pause()
    self.timeScale = 0
end

function Timer:resume()
    self.timeScale = 1.0
end

function Timer:getTime()
    return self.elapsedTime
end
